﻿// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Unlit/shui2"
{
	Properties
	{
		_XSpeed("XSpeed",float) = 0.2
		_YSpeed("YSpeed",float) = 0.2
		_Color ("Color", Color) = (1,1,1,1)
		_NormalMap("Normal Map",2D) =""{}
		_DefTex("Def Tex",2D)=""{}
		_ZheSheBi("ZheSheBi",Range(0.01,2))=0.5
		_HHH("HHH",float)=1
		_Hehua("hehua",2D)=""{}
	}
	SubShader
	{		
		Tags { "RenderType"="Transparent" "LightMode"="ForwardBase" "Queue" = "Transparent+100"}
		LOD 100
		GrabPass {"_GPTex"}		
		Pass
		{			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;				
				float3 noraml:NORMAL;
				float4 texcoord:TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float3 wViewDir:TEXCOORD0;
				float3 wNormal:TEXCOORD1;
				float4 scrPos:TEXCOORD2;
				float4 proj:TEXCOORD3;
				float4 uv:TEXCOORD4;
				float3 wPos:TEXCOORD5;			
			};

			sampler2D _Hehua;
			float4 _Hehua_ST;
			sampler2D _GPTex;
			float4 _GPTex_ST;
			sampler2D _NormalMap;
			float4 _NormalMap_ST;
			float4 _NormalMap_TexelSize;
			fixed4 _Color;
			float _ZheSheBi;
			float _HHH;
			float _XSpeed;
			float _YSpeed;
			sampler2D _DefTex;
			float4 _DefTex_ST;
			sampler2D _CameraDepthTexture;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);

				o.wViewDir =WorldSpaceViewDir(v.vertex);
				o.wNormal = mul(unity_ObjectToWorld,v.noraml);
				o.scrPos = ComputeGrabScreenPos(o.vertex);
				o.proj = ComputeScreenPos(o.vertex);  
				COMPUTE_EYEDEPTH(o.proj.z); 
				o.wPos = mul(unity_ObjectToWorld,v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.texcoord,_NormalMap);
				o.uv.zw = TRANSFORM_TEX(v.texcoord,_DefTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.proj)));  

				half m_depth = LinearEyeDepth(tex2Dproj (_CameraDepthTexture, i.proj).r);
				half deltaDepth = sceneZ - i.proj.z;//计算深度差 
				//return fixed4(i.proj.r,i.proj.r,i.proj.r,1);
				//if(deltaDepth < 0)
				//{					
				//	clip(-1);
				//	//return tex2D(_GPTex, i.scrPos.xy / i.scrPos.w);
				//	return fixed4(0,0,0,1);
				//}
				//else
				{

					float3 nm1 = UnpackNormal(tex2D(_NormalMap,i.uv + float2(_XSpeed,_YSpeed) * _Time.y  ));
					float3 nm2 = UnpackNormal(tex2D(_NormalMap,i.uv - float2(_XSpeed,_YSpeed) * _Time.y  ));
					i.wNormal = normalize (i.wNormal + (nm1 + nm2)/4);
					//float3 f3 = normalize( cross( cross(i.wNormal,fixed3(-2,-0.11,-2)),i.wNormal)) ;
					float3 f3 = normalize( cross( cross(i.wNormal,-i.wViewDir),i.wNormal)) ;
					float4 f4 = mul(UNITY_MATRIX_VP,fixed4(f3,0));
					
					f4.xy *= 5 * (1 - _ZheSheBi) * deltaDepth;
					f4.xy = f4.xy/f4.w;
					//f4.xy = f4.xy * 0.5 + 0.5;
					//return fixed4(f4.x,f4.x,f4.x,1);

					i.scrPos.xy = float2(-f4.x,f4.y) * 0.1 + i.scrPos.xy;
					fixed4 wc = (tex2D(_DefTex,i.uv.zw + float2(_XSpeed,_YSpeed) * _Time.y) * tex2D(_DefTex,i.uv.zw - float2(_XSpeed,_YSpeed) * _Time.y));
					fixed4 rc = (tex2D(_GPTex, i.scrPos.xy / i.scrPos.w) * 0.9  + wc * 0.2);
					return lerp(_Color,rc,(5.25-i.wPos.y)/0.1);
				}
			}
			ENDCG
		}
	}
}
